import javax.swing.*;
import java.awt.*;

public class GamePanel extends JPanel {
    protected boolean[][] masks = new boolean[9][9];
    private int maskNum = 0;
    private int[][] puzzle = new int[9][9];
    public static final int GRID_SIZE = 9;
    public static final int SUBGRID_SIZE = 3;


    public static final int CELL_SIZE = 60;
    public static final int CANVAS_WIDTH = CELL_SIZE * GRID_SIZE;
    public static final int CANVAS_HEIGHT = CELL_SIZE * GRID_SIZE;
    public static final Color OPEN_CELL_BGCOLOR = Color.YELLOW;
    public static final Color CLOSED_CELL_BGCOLOR = new Color(240, 240, 240);
    public static final Color CLOSED_CELL_TEXT = Color.BLACK;
    public static final Font FONT_NUMBERS = new Font("Monospaced", Font.BOLD, 20);

    private JTextField[][] tfCells = new JTextField[GRID_SIZE][GRID_SIZE];

    public GamePanel(boolean[][] masks) {
        this.masks = masks;
        GenerateBoard generateBoard = new GenerateBoard();
        puzzle = generateBoard.generation();
        for(int i=0;i<9;i++){
            for(int j=0;j<9;j++){
                if(masks[i][j] == true) maskNum++;
            }
        }

        this.setLayout(new GridLayout(GRID_SIZE, GRID_SIZE)); // 9x9 GridLayout

        ;
        for (int row = 0; row < GRID_SIZE; ++row) {
            for (int col = 0; col < GRID_SIZE; ++col) {
                tfCells[row][col] = new JTextField();      // Allocate element of array
                this.add(tfCells[row][col]);               // ContentPane adds JTextField
                if (masks[row][col]) {
                    tfCells[row][col].setText("");         // set to empty string
                    tfCells[row][col].setEditable(true);
                    tfCells[row][col].setBackground(OPEN_CELL_BGCOLOR);
                    InputListener inputListener = new InputListener(this,row,col);
                    tfCells[row][col].addActionListener(inputListener);

                } else {
                    tfCells[row][col].setText(puzzle[row][col] + "");
                    tfCells[row][col].setEditable(false);
                    tfCells[row][col].setBackground(CLOSED_CELL_BGCOLOR);
                    tfCells[row][col].setForeground(CLOSED_CELL_TEXT);
                }
                // Beautify all the cells
                tfCells[row][col].setHorizontalAlignment(JTextField.CENTER);
                tfCells[row][col].setFont(FONT_NUMBERS);
            }
        }

        // Set the size of the content-pane and pack all the components under this container.
        this.setPreferredSize(new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT));
    }

    public void isSelectedCellInputCorrect(int row,int col) {
            String num = tfCells[row][col].getText();
            if(Integer.parseInt(num) == puzzle[row][col]) {
                tfCells[row][col].setBackground(Color.GREEN);
                maskNum--;
            }
            else tfCells[row][col].setBackground(Color.RED);
            //数字超标警告
            if(Integer.parseInt(num)>9 || Integer.parseInt(num)<1)
                JOptionPane.showMessageDialog(null ,"Number out of range !");
    }


    public void isGameOver(){
        if(maskNum == 0) JOptionPane.showMessageDialog(null,"CONGRATULATION !");
    }


}
